package com.itheima.common;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    /*
    *插入时填充
    *
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        Long empId =  ReggieContext.get();
        // 官网的方式
        this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now());
        this.strictInsertFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
        this.strictInsertFill(metaObject, "createUser", Long.class, empId);
        this.strictInsertFill(metaObject, "updateUser", Long.class, empId);

    }

    /*
     *更新时填充
     *
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        Long empId =  ReggieContext.get();
            // strictUpdateFill 方法 如果你的参数有值 就不帮你填充
        // 只有字段为空 为null的时候才会帮你填充
          this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
          this.strictUpdateFill(metaObject, "updateUser", Long.class, empId);
//        // 这里强制set 会忽略原始的数据
//        metaObject.setValue("updateUser",1L);
//        metaObject.setValue("updateTime",LocalDateTime.now());
    }
}
